package com.apes.fn.scm.purchaseReturn.model;

import com.apes.fn.tms.model.Site;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.scm.purchaseReturn.model
 * @ClassName: PurchaseAfter
 * @Author: ljp
 * @CreateTime: 2021-08-04 15:54
 * @Description: 售后单
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PurchaseAfter.CONST_TABLE_NAME)
public class PurchaseAfter extends Model {
    public static final String CONST_TABLE_NAME = "purchase_after";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "售后单", padding = 8, useDateRange=true)
    private String id;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "location_id", referencedColumnName = "id")
    @Comment("仓库")
    private Location location;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "site", referencedColumnName = "id")
    @Comment("售后点")
    private Site site;

    @Column(name = "note")
    @Comment("备注")
    private String note;

    @Column(name = "state", length = 10, nullable = false)
    @States(name = "订单状态", values = {
            @State(id = "create", name = "新建"),
            @State(id = "done", name = "完成")})
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "apply_uid", referencedColumnName = "id")
    @Comment("提交人")
    private PartyRole applyUid;

    @Column(name = "date_apply")
    @Comment("提交日期")
    private Date dateApply;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_uid", referencedColumnName = "id")
    @Comment("删除人")
    private PartyRole deleteUid;

    @Column(name = "date_delete")
    @Comment("删除日期")
    private Date dateDelete;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "return_reason", referencedColumnName = "id")
    @Comment("返厂原因")
    private Enumeration returnReason;

    @OneToMany(mappedBy = "purchaseAfter", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<PurchaseAfterItem> purchaseAfterItems = new HashSet<>();

    public Set<PurchaseAfterItem> getPurchaseAfterItems() {
        for (PurchaseAfterItem item : purchaseAfterItems) {
            item.setPurchaseAfter(this);
        }
        return purchaseAfterItems;
    }
}
